package operate

import (
	"context"
	"fmt"

	"MBTI/api/internal/svc"
	"MBTI/api/internal/types"
	"MBTI/rpc/login/log"

	"github.com/zeromicro/go-zero/core/logx"
)

type LoginLogic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

func NewLoginLogic(ctx context.Context, svcCtx *svc.ServiceContext) *LoginLogic {
	return &LoginLogic{
		Logger: logx.WithContext(ctx),
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}

func (l *LoginLogic) Login(req *types.LoginReq) (resp *types.Reply, err error) {
	isExitResp, err := l.svcCtx.LoginRpc.IsExit(l.ctx, &log.IsExitReq{StudentNo: req.StudentNo})
	if err != nil {
		return nil, err
	}
	if !isExitResp.IsSuccess {
		return &types.Reply{Code: 110, Msg: "用户名不存在"}, nil
	}
	fmt.Println("成功验证是否存在完成")
	isSuccess, err := l.svcCtx.LoginRpc.Login(l.ctx, &log.LoginReq{StudentNo: req.StudentNo, Password: req.Password})
	if err != nil {
		return nil, err
	}
	if !isSuccess.IsSuccess {
		return &types.Reply{Code: 111, Msg: "密码错误"}, nil
	}
	return &types.Reply{Code: 100,
		Data: map[string]interface{}{
			"StudentNo": req.StudentNo,
		},
		Msg: "登陆成功！"}, nil
}
